package com.example.demo.Service.lmp;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.Dto.AddItemDTO;
import com.example.demo.Info.CartItemInfo;
import com.example.demo.Mapper.CartItemMapper;
import com.example.demo.Service.CartItemService;
import com.example.demo.Service.GoodsService;
import com.example.demo.Util.ThreadLocallUtil;
import com.example.demo.VO.CartInfoVo;
import com.example.demo.po.CartItem;
import com.example.demo.po.Product;
import com.example.demo.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CartItemServiceImp extends ServiceImpl<CartItemMapper, CartItem> implements CartItemService {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Transactional
    public void addCart(AddItemDTO addItemDTO ,Long cartId) {
        Product product = goodsService.getById(addItemDTO.getProductId());
        if (product == null){
            throw new RuntimeException("商品不存在");
        }
        redisTemplate.opsForValue().getAndDelete("cart:"+cartId);
        CartItem cartItem = BeanUtil.copyProperties(addItemDTO, CartItem.class);
        cartItem.setCartId(cartId);
        cartItem.setCreateTime(LocalDateTime.now());
        cartItem.setUpdateTime(LocalDateTime.now());
        cartItem.setStatus(0);
        boolean b = save(cartItem);
        if (!b){
            throw new RuntimeException("添加购物车失败");
        }
    }

    @Override
    public void deleteCart(Long l,Long userId) {
        boolean update = update(new UpdateWrapper<CartItem>()
                            .eq("cart_id", l)
                            .eq("status", 0)
                            .setSql("status=1"));
        if (!update){
            throw new RuntimeException("删除购物车失败");
        }
        redisTemplate.opsForValue().getAndDelete("cart:"+userId);
    }


    @Override
    public CartInfoVo getCartItem(CartInfoVo getcart) {
        List<CartItem> cartId = list(new QueryWrapper<CartItem>().eq("cart_id", getcart.getId()));
        List<CartItemInfo> map = CollUtil.map(cartId, a -> {
            CartItemInfo b = new CartItemInfo();
            BeanUtil.copyProperties(a, b);
            return b;
        }, true);
        getcart.setCartItems(map);
        return getcart;
    }

    @Override
    public com.example.demo.VO.CartItem idgetCartItem(Long id) {
        Object o = redisTemplate.opsForValue().get("cart:" + id);
        if (o != null){
            CartInfoVo cartInfoVo = JSONUtil.toBean(o.toString(), CartInfoVo.class);
            List<CartItemInfo> cartItems = cartInfoVo.getCartItems();
            for (CartItemInfo cartItem : cartItems){
                    if (cartItem.getId()== id){
                        com.example.demo.VO.CartItem cartItem1 = BeanUtil.copyProperties(cartItem, com.example.demo.VO.CartItem.class);
                        return cartItem1;
                }
            }
        }
        CartItem byId = getById(id);
        if (byId==null){
            throw new RuntimeException("购物车不存在");
        }
         return  com.example.demo.VO.CartItem.builder()
                .cartItemId(id)
                .productId(byId.getProductId())
                .quantity(byId.getQuantity())
                .build();
    }
}
